package output;

import PackagePoseidon.DirectionData;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Michel Uncini 01/08/2010
 *
 * The class Direction is used to make the user observe the joystick movements,
 * through a simple interface made of the buttons UP, DOWN, LEFT, RIGHT,
 * FORWARD. These buttons will change color each time the joytick changes
 * direction.
 *
 */
public class Direction extends JPanel implements Observer {
	
	//control pannel is a JPanel; the constructor builds the panel
	 

 
	JPanel direction = new JPanel();
    private JButton upButton;
    private JButton downButton;
	private JButton leftButton  ;
	private JButton rightButton  ;
	private JButton forwardButton;

  
    
    private void intDirection(){
        direction.setLayout(new BorderLayout());
        /*Add the two panels to the JFrame*/

         upButton = new JButton("UP");
		 downButton = new JButton("DOWN");
		 leftButton = new JButton("LEFT");
		 rightButton = new JButton("RIGHT");
		 forwardButton = new JButton("FORWARD");



		direction.add(upButton,  BorderLayout.PAGE_START);
		direction.add(downButton,  BorderLayout.PAGE_END);
		direction.add(leftButton,  BorderLayout.LINE_START);
		direction.add(forwardButton,  BorderLayout.CENTER);
		direction.add(rightButton,  BorderLayout.LINE_END);

    }
	public Direction () {
	
		
        
        this.setLayout(new GridLayout(1,0) );
		this.setPreferredSize(new Dimension(250, 500));
        
     
        intDirection();

        this.add(direction);

	}
	

    private void onNewData(Object data) {

        if (data instanceof DirectionData){
            DirectionData dataDir = (DirectionData)data;
            
            switch (Integer.parseInt(dataDir.direction)) {
                case 0: //FORWARD
                        forwardButton.setBackground(Color.RED);
                        upButton.setBackground(Color.CYAN);
                        downButton.setBackground(Color.CYAN);
                        rightButton.setBackground(Color.CYAN);
                        leftButton.setBackground(Color.CYAN);
                        break;
                case 1: //UP
                        forwardButton.setBackground(Color.CYAN);
                        upButton.setBackground(Color.RED);
                        downButton.setBackground(Color.CYAN);
                        rightButton.setBackground(Color.CYAN);
                        leftButton.setBackground(Color.CYAN);
                        break;
                case 2: //DOWN
                        forwardButton.setBackground(Color.CYAN);
                        upButton.setBackground(Color.CYAN);
                        downButton.setBackground(Color.RED);
                        rightButton.setBackground(Color.CYAN);
                        leftButton.setBackground(Color.CYAN);
                        break;
                case 3: //LEFT
                        forwardButton.setBackground(Color.CYAN);
                        upButton.setBackground(Color.CYAN);
                        downButton.setBackground(Color.CYAN);
                        rightButton.setBackground(Color.CYAN);
                        leftButton.setBackground(Color.RED);
                        break;
                case 4: //RIGHT
                        forwardButton.setBackground(Color.CYAN);
                        upButton.setBackground(Color.CYAN);
                        downButton.setBackground(Color.CYAN);
                        rightButton.setBackground(Color.RED);
                        leftButton.setBackground(Color.CYAN);
                        break;
                case 10: //NO DIRECTION
                        forwardButton.setBackground(Color.CYAN);
                        upButton.setBackground(Color.CYAN);
                        downButton.setBackground(Color.CYAN);
                        rightButton.setBackground(Color.CYAN);
                        leftButton.setBackground(Color.CYAN);
                        break;
            }
        }
    }
    public void update(Observable o, Object arg){
        onNewData(arg);
    }
}
